<?php

/**
 * This is the model class for table "Usuario".
 *
 * The followings are the available columns in table 'Usuario':
 * @property integer $id
 * @property string $idUsuarioFB
 * @property string $nomeUsuarioFB
 * @property integer $tipoUsuario
 * @property string $email
 * @property string $senha
 * @property integer $newsletter
 * @property string $status
 *
 * The followings are the available model relations:
 * @property Autor[] $autors
 * @property AvaliacaoOA[] $avaliacaoOAs
 * @property OA[] $oAs
 * @property TipoUsuario $tipoUsuario0
 */
class Usuario extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Usuario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Usuario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tipoUsuario, newsletter', 'required'),
			array('tipoUsuario, newsletter', 'numerical', 'integerOnly'=>true),
			array('idUsuarioFB', 'length', 'max'=>20),
			array('nomeUsuarioFB', 'length', 'max'=>60),
			array('email', 'length', 'max'=>80),
			array('senha', 'length', 'max'=>32),
			array('status', 'length', 'max'=>1),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, idUsuarioFB, nomeUsuarioFB, tipoUsuario, email, senha, newsletter, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'autors' => array(self::HAS_MANY, 'Autor', 'idUsuario'),
			'avaliacaoOAs' => array(self::HAS_MANY, 'AvaliacaoOA', 'idUsuario'),
			'oAs' => array(self::HAS_MANY, 'OA', 'idUsuario'),
			'tipoUsuario0' => array(self::BELONGS_TO, 'TipoUsuario', 'tipoUsuario'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'idUsuarioFB' => 'ID do Facebook',
			'nomeUsuarioFB' => 'Nome',
			'tipoUsuario' => 'Tipo de Usuário',
			'email' => 'Email',
			'senha' => 'Senha',
			'newsletter' => 'Newsletter',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('idUsuarioFB',$this->idUsuarioFB,true);
		$criteria->compare('nomeUsuarioFB',$this->nomeUsuarioFB,true);
		$criteria->compare('tipoUsuario',$this->tipoUsuario);
		$criteria->compare('email',$this->email,true);
//		$criteria->compare('senha',$this->senha,true);
		$criteria->compare('newsletter',$this->newsletter);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	protected function beforeSave(){
		if(strlen($this->senha) < 32)
			$this->senha = md5($this->senha);
		return true;	
	}
}
